package cn.tedu.boot32.controller;

import cn.tedu.boot32.entity.User;
import cn.tedu.boot32.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wxd
 * @version 1.0
 */
@RestController
public class UserController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user) {
        if (mapper.selectByUsername(user.getUsername()) != null) {
            // 2代表用户名已存在
            return 2;
        }
        if ("".equals(user.getUsername()) || "".equals(user.getPassword())) {
            // 3代表输入用户名或密码为空
            return 3;
        }
        mapper.insert(user);
        // 1代表注册成功
        return 1;
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user) {
        User u = mapper.selectByUsername(user.getUsername());
        if (u != null) {
            if (u.getPassword().equals(user.getPassword())) {
                // 1代表登录成功
                return 1;
            }
            // 2代表密码错误
            return 2;
        }
        // 3代表用户名不存在
        return 3;
    }
}
